vcдdllȻʹsp_addextendedproc         չĴ洢          
       һȥдһչ洢          
              
       /*ָIPַMacַ*/          
              
       #include          <stdafx.h>           
       #include          "stdio.h "                
       #include          "stdlib.h "                
       #include          "Winsock2.h "                
       #include          "iphlpapi.h "                
                          
       #pragma         comment         (         lib,          "ws2_32.lib "         )                
       #pragma         comment         (         lib,          "Iphlpapi.lib "         )                
                          
       #define         XP_NOERROR                                                                                       0          
       #define         XP_ERROR                                                                                                   1          
       #define         MAXCOLNAME   25          
       #define         MAXNAME   255          
       #define         MAXTEXT   255          
              
       #ifdef         __cplusplus          
       extern          "C "         {          
       #endif          
              
       RETCODE         __declspec(dllexport)         xp_proc5(SRV_PROC         *srvproc);          
              
       #ifdef         __cplusplus          
       }          
       #endif          
              
       RETCODE         __declspec(dllexport)         xp_proc5(SRV_PROC         *srvproc)          
       {          
              
                               DBSMALLINT         i         =         0;          
                               DBCHAR         colname[MAXCOLNAME];          
       DBCHAR         spText[MAXTEXT];          
       DBCHAR         spIP[MAXTEXT];          
       DBCHAR         spHostName[MAXTEXT];          
       DBCHAR         spMac[18];          
       DBCHAR         szFileName[MAX_PATH+1];          
              
       struct         hostent         *remoteHostent;          
       int         numberOfHost         =         1;          
                               int         nArgs         =         srv_rpcparams(srvproc);          
       if         (nArgs> 0)          
       {          
       //ʼSOCKET          
       WSADATA         wsaData;          
       int         iRet         =         WSAStartup(MAKEWORD(2,1),         &wsaData);          
       if         (         iRet         !=         0         )          
       exit(         0         );          
              
       int         paramCount=srv_rpcparams(srvproc);//          
       if(paramCount!=1)          
       {          
       return         XP_ERROR;          
       }          
              
       BYTE                                                                           bType;          
       unsigned         long                     cbMaxLen;          
       unsigned         long                     cbActualLen;          
       BOOL                                                                           fNull;          
              
       int         ret=srv_paraminfo(srvproc,1,&bType,&cbMaxLen,&cbActualLen,NULL,&fNull);          
              
       if         (cbActualLen){          
       ZeroMemory(szFileName,         MAX_PATH+1);          
       memcpy(szFileName,         srv_paramdata(srvproc,         1),         cbActualLen);          
       }          
       else{          
       return         XP_ERROR;          
       }          
              
       int         nRemoteAddr=inet_addr((const         char*)szFileName);          
       _snprintf(spText,         MAXTEXT,          "%s         ʾչ洢 ",         szFileName);          
       srv_sendmsg(          
       srvproc,          
       SRV_MSG_INFO,          
       0,          
       (DBTINYINT)0,          
       (DBTINYINT)0,          
       NULL,          
       0,          
       0,          
       spText,          
       SRV_NULLTERM);          
              
       remoteHostent=         (struct         hostent*)malloc(         sizeof(struct         hostent         ));          
       struct         in_addr         sa;          
       for         (         int         i         =         0;         i          <         numberOfHost;         i         ++         )          
       {          
       //ȡԶ̻          
       sa.s_addr         =         nRemoteAddr;          
       _snprintf(         spIP,MAXTEXT, "%s\0 ",inet_ntoa(         sa         )         );//IPַ          
              
       remoteHostent         =         gethostbyaddr(         (char*)&nRemoteAddr,4,         AF_INET         );          
       if         (         remoteHostent         )          
       _snprintf(spHostName,MAXTEXT, "%s\0 ",remoteHostent-> h_name);          
       else          
       _snprintf(spHostName,5, "null ");          
              
       //ARPѯԶMACַ          
       unsigned         char         macAddress[6];          
       ULONG         macAddLen         =         6;          
       iRet=SendARP(nRemoteAddr,         (unsigned         long)NULL,(PULONG)&macAddress,         &macAddLen);          
       if         (         iRet         ==         NO_ERROR         )          
       {          
       sprintf(spMac, "%02X-%02X-%02X-%02X-%02X-%02X ",macAddress[0],macAddress[1],macAddress[2],macAddress[3],macAddress[4],macAddress[5]);          
       }          
       else          
       sprintf(spMac, "null ");          
       nRemoteAddr         =         htonl(         ntohl(         nRemoteAddr         )         +         1         );          
       }          
       }          
              
       //          
                               _snprintf(colname,         MAXCOLNAME,          "IP ");          
                               srv_describe(srvproc,         1,         colname,         SRV_NULLTERM,         SRVCHAR,         MAXTEXT,         SRVCHAR,         0,         NULL);          
              
                               _snprintf(colname,         MAXCOLNAME,          "HOSTNAME ");          
                               srv_describe(srvproc,         2,         colname,         SRV_NULLTERM,         SRVCHAR,         MAXNAME,         SRVCHAR,         0,         NULL);          
              
                               _snprintf(colname,         MAXTEXT,          "MAC ");          
                               srv_describe(srvproc,         3,         colname,         SRV_NULLTERM,         SRVCHAR,         MAXTEXT,         SRVCHAR,         0,         NULL);          
              
                               //          
       srv_setcoldata(srvproc,         1,         spIP);          
                               srv_setcollen(srvproc,         1,         static_cast <int> (strlen(spIP)));          
       srv_setcoldata(srvproc,         2,         spHostName);          
                               srv_setcollen(srvproc,         2,         static_cast <int> (strlen(spHostName)));          
       srv_setcoldata(srvproc,         3,         spMac);          
                               srv_setcollen(srvproc,         3,         static_cast <int> (strlen(spMac)));          
              
       //                   
                               srv_sendrow(srvproc);          
              
       //         ڷѴ          
       srv_senddone(srvproc,         SRV_DONE_MORE         |         SRV_DONE_COUNT,         (DBUSMALLINT)0,         (DBINT)i);          
              
       return         XP_NOERROR         ;          
       }    
  
****************************************** 
ú,ӵmssql          
       sp_addextendedproc          'xp_proc5 ', 'dllpath '          
              
       ÷          
       xp_proc5          '10.50.0.1 '         --ָIPַ          
              
       Ч          
              
       IP                                                                           HOSTNAME                                                                                 MAC          
       --------------------------------------------------------          
       10.50.80.22                     sunshine                                                                                 00-80-C8-EE-11-03                                        
              
       жصķ          
       sp_dropextendedphroc          'xp_proc5 ' 
Դŵ(http://bbs.firnow.com) ϸοhttp://bbs.firnow.com/dview6t129872.html


/***************************************************************** 
  NAME:                 xp_sendmymsg 
*****************************************************************/ 
//   This   is   an   extended   procedure   DLL   built   with   Open   Data 

//   The   Transact-SQL   script   xp_sendmymsg.sql   installs   and   exercises   the   extended 
//   stored   procedure. 

#include   <stdafx.h> 
#include   <stdlib.h> 

#define   XP_NOERROR                             0 
#define   XP_ERROR                                 1 

#define   MAX_SERVER_ERROR   20000 
#define   xp_sendmymsg_ERROR   MAX_SERVER_ERROR+1 

ULONG   __GetXpVersion() 

{ 
        return   ODS_VERSION; 
} 

#ifdef   __cplusplus 
extern   "C "   { 
#endif 

void   printUsage   (SRV_PROC   *pSrvProc); 
void   printError   (SRV_PROC   *pSrvProc,   CHAR*   szErrorMsg); 

RETCODE   __declspec(dllexport)   xp_sendmymsg(SRV_PROC   *pSrvProc); 

#ifdef   __cplusplus 
} 
#endif 

//   send   XP   main 
RETCODE   __declspec(dllexport)   xp_sendmymsg(SRV_PROC   *pSrvProc) 
{ 
        BYTE                 bType; 
        ULONG               cbMaxLen; 
        ULONG               cbActualLen; 
        BOOL                 fNull; 

char* target; 
int   eventID; 
char* str; 

        STS status; 
        static   io_type io   =   channel; 

        //   get   the   number   of   input   parameters.     There   should   be   three. 
        if   (srv_rpcparams(pSrvProc)   !=   3) 
{ 
                //   Send   error   message   and   return 
                // 
                printUsage   (pSrvProc); 
                return   (XP_ERROR); 
} 


//Retrieve   1st   input   parameter   (io   handle   name) 
        if   (srv_paraminfo(pSrvProc,   1,   &bType,   &cbMaxLen,   &cbActualLen, 
                NULL,   &fNull)   ==   FAIL) 
                { 
                printError   (pSrvProc,   "srv_paraminfo   failed   1 "); 
  return   (XP_ERROR); 
} 
        //   If   there 's   data,   then   allocate   memory   and   get   it. 
        if   (fNull   ==   0) 
                { 
                target   =   (char   *)   malloc(cbActualLen+1); 
                if   (target   ==   NULL) 
{ 
printError   (pSrvProc,   "Memory   allocation   error   1 "); 
return   (XP_ERROR); 
} 

                if   (srv_paraminfo(pSrvProc,   1,   &bType,   &cbMaxLen, 
                        &cbActualLen,   (BYTE*)   target,   &fNull)   ==   FAIL) 
{ 
printError   (pSrvProc,   "Failed   to   retrieve   parameter   1 "); 
return   (XP_ERROR); 
} 
                } 

target[cbActualLen]   =   0;     //put   NULL   on   end   of   char 


//Retrieve   2nd   input   parameter   (   event   number) 
        if   (srv_paraminfo(pSrvProc,   2,   &bType,   &cbMaxLen, 
                &cbActualLen,   (BYTE*)   &eventID,   &fNull)   ==   FAIL) 
{ 
printError   (pSrvProc,   "Failed   to   retrieve   parameter   2 "); 
//   Free   allocated   memory 
free   (target); 
return   (XP_ERROR); 
} 


//Retrieve   3rd   input   parameter   (   text) 
        if   (srv_paraminfo(pSrvProc,   3,   &bType,   &cbMaxLen,   &cbActualLen, 
                NULL,   &fNull)   ==   FAIL) 
                { 
                printError   (pSrvProc,   "srv_paraminfo   failed   3 "); 
  return   (XP_ERROR); 
} 
        //   If   there 's   data,   then   allocate   memory   and   get   it. 
        if   (fNull   ==   0) 
                { 
                str   =   (char   *)   malloc(cbActualLen+1); 
                if   (str   ==   NULL) 
{ 
printError   (pSrvProc,   "Memory   allocation   error   3 "); 
return   (XP_ERROR); 
} 

                if   (srv_paraminfo(pSrvProc,   3,   &bType,   &cbMaxLen, 
                        &cbActualLen,   (BYTE*)   str,   &fNull)   ==   FAIL) 
{ 
printError   (pSrvProc,   "Failed   to   retrieve   parameter   3 "); 
        free   (target); 
return   (XP_ERROR); 
} 
                } 

str[cbActualLen]   =   0;     //put   NULL   on   end   of   char 


        //   Send   a   message   to   the   receiver,replace   it   use   your   communication   code 
                status   =   Send_Mymsg   (io,   0,   0,   channel,   eventID,   str,   FALSE,   FALSE); 

        //   Free   allocated   memory 
        free   (target); 
free   (str); 

//   Now   return   the   number   of   rows   processed 
        srv_senddone(pSrvProc,   SRV_DONE_MORE   |   SRV_DONE_COUNT,   (DBUSMALLINT)0,   (DBINT)0); 

return   XP_NOERROR   ; 
} 


//   send   XP   usage   info   to   client 
void   printUsage   (SRV_PROC   *pSrvProc) 
{ 
        srv_sendmsg(pSrvProc,   SRV_MSG_ERROR,   xp_sendmymsg_ERROR,   SRV_INFO,   1, 
                        NULL,   0,   (DBUSMALLINT)   __LINE__, 
                        "Usage:   EXECUTE   xp_sendmymsg   'target ',   'event   name ',   'str ' ", 
                        SRV_NULLTERM); 
        srv_senddone(pSrvProc,   (SRV_DONE_ERROR   |   SRV_DONE_MORE),   0,   0); 
} 

//   send   szErrorMsg   to   client 
void   printError   (SRV_PROC   *pSrvProc,   CHAR*   szErrorMsg) 
{ 
        srv_sendmsg(pSrvProc,   SRV_MSG_ERROR,   xp_sendmymsg_ERROR,   SRV_INFO,   1, 
                        NULL,   0,   (DBUSMALLINT)   __LINE__, 
                        szErrorMsg, 
                        SRV_NULLTERM); 

        srv_senddone(pSrvProc,   (SRV_DONE_ERROR   |   SRV_DONE_MORE),   0,   0); 
} 

------ 
